/*
  Scoruino - Scoringboard for the arduino

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <WProgram.h>
#include <NESpad/NESpad.h>
#include <LiquidCrystal/LiquidCrystal.h>
#include <Ports.h>

//Initialization functions
void rfid_init(void);
char *rfid_read(void);

//loop functions
void loopNes(void);


//Globals for program use
byte NES_STATE;
NESpad NES_PAD = NESpad();
LiquidCrystal big_disp = LiquidCrystal(big_disp_rs, rw_dummy_pin, big_disp_enable, big_disp_db4, big_disp_db5, big_disp_db6, big_disp_db7);
LiquidCrystal l_disp = LiquidCrystal(l_disp_rs, rw_dummy_pin, l_disp_enable, l_disp_db4, l_disp_db5, l_disp_db6, l_disp_db7);
LiquidCrystal r_disp = LiquidCrystal(r_disp_rs, rw_dummy_pin, r_disp_enable, r_disp_db4, r_disp_db5, r_disp_db6, r_disp_db7);
int counter=0;
int val = 0;
void disp_init(void);
void setup(void){
    //initialize NES controller
    rfid_init();
    disp_init();
    Serial.begin(57600);
}

void disp_init(void){
    r_disp.clear();
    l_disp.clear();
    big_disp.clear();
}

void loop(void){
    //all the loops for the updates
    loopNes();
    big_disp.setCursor(0,0);
        big_disp.print("v1, time ");
    big_disp.print(counter);
    big_disp.setCursor(0,1);

    char *rfidId = rfid_read();
    if (rfidId) {
        Serial.println(rfidId);
        big_disp.setCursor(0,2);
        big_disp.print(rfidId);
        big_disp.setCursor(0,3);
        big_disp.print("last rfid at ");
        big_disp.print(counter);
        delay(500);
    }
    delay(100);
    counter++;
}

void loopNes(void){
    NES_STATE = NES_PAD.buttons();
    Serial.print(NES_STATE, BIN);
}



int rfid_val = 0; 
char rfid_code[RFID_NUMBYTES+1]; 
int rfid_bytesread = 0;

void rfid_init(void){
    Serial.println("Initializing RFID");
    rfid_code[RFID_NUMBYTES] = 0;
    RFID_SERIAL.begin(2400);
    pinMode(RFID_PORT, OUTPUT);
    digitalWrite(RFID_PORT, LOW);
}

void rfid_reset() { // visual noficiation omololo
    digitalWrite(RFID_PORT, HIGH);
    delay(100);
    digitalWrite(RFID_PORT, LOW);
}

char *rfid_read() {
    char *res=0;
    while (RFID_SERIAL.available()>0) {
        int avail = RFID_SERIAL.available();
        Serial.print("avail ");
        Serial.println(avail);
        if(RFID_SERIAL.read() != 10) {
            continue;
        }
        rfid_bytesread = 0; 
        while (rfid_bytesread<RFID_NUMBYTES) {
            if (RFID_SERIAL.available() > 0) { 
                rfid_val = RFID_SERIAL.read(); 
                if((rfid_val == 10)||(rfid_val == 13)) { // if header or stop bytes before the 10 digit reading 
                    return 0;
                }
                rfid_code[rfid_bytesread] = rfid_val;         // add the digit           
                rfid_bytesread++;                   // ready to read next digit  
            } 
        } 
        res = rfid_code;
    }
    if (res) {
        rfid_reset();
    }
    return res;
} 




int main(void) {
    init();
    setup();
    for (;;)
        loop();
    return 0;
}

